<?php
/*
  $Id: attributes.php,v 1.3 2004/03/16 22:36:34 vj Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2002 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
  $languages = tep_get_languages();

 // $products_id = (isset($_GET['pID']) ? (int)$_GET['pID'] : '');

  if (isset($_GET['pID'])) {
    $products_id = $_GET['pID'] ;
    }else if (isset($_POST['pID'])){
    $products_id = $_POST['pID'] ;
    } else {
    $products_id = '' ;
    }

   if (isset($_GET['action'])) {
     $action = $_GET['action'] ;
     }else if (isset($_POST['action'])){
     $action = $_POST['action'] ;
     } else {
     $action = '' ;
    }

   if (isset($_GET['option_toggle'])) {
     $option_toggle = $_GET['option_toggle'] ;
     }else if (isset($_POST['option_toggle'])){
     $option_toggle = $_POST['option_toggle'] ;
     } else {
     $option_toggle = '' ;
    }

///get submit button value
   $redirect = isset($_POST['submit']) ? $_POST['submit'] : '';

// compare to current language define and change to a case label
if ($redirect == IMAGE_SAVE_FINISH){
$redirect = 'save_finish';
}
if ($redirect == IMAGE_SAVE_RELOAD){
$redirect = 'save_reload';
}
if ($redirect == IMAGE_SAVE_EDIT_PRODUCT){
$redirect = 'save_edit_product';
}
if ($redirect == IMAGE_CANCEL){
$redirect = 'cancel';
}
if ($redirect == IMAGE_CANCEL_EDIT_PRODUCT){
$redirect = 'cancel_edit_product';
}

//submits that do not need to be processed
 switch ($redirect) {
     case 'cancel':
        $_SESSION['sess_option'] = '';
       tep_redirect(tep_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID']));
    case 'cancel_edit_product':
        $_SESSION['sess_option'] = '';
       tep_redirect(tep_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID'] . '&action=new_product'));
      break;
  }

  switch ($action) {
    case 'edit':
      if (!empty($option_toggle)) {

        foreach ($option_toggle as $option_id) {

          if ((isset($_POST['values'][$option_id]))  && (!empty($_POST['values'][$option_id])) ) {

            foreach ($_POST['values'][$option_id] as $value_id) {

              $price_prefix = (tep_not_null($_POST['values_price_prefix'][$option_id][$value_id]) && (tep_db_prepare_input($_POST['values_price_prefix'][$option_id][$value_id]) == '-')) ? '-' : '+';
              $price = tep_not_null($_POST['values_price'][$option_id][$value_id]) ? tep_db_prepare_input($_POST['values_price'][$option_id][$value_id]) : '0';
              $sort_order = tep_not_null($_POST['values_sort_order'][$option_id][$value_id]) ? tep_db_prepare_input($_POST['values_sort_order'][$option_id][$value_id]) : '0';

              $sql_data_array = array('options_values_price' => $price,
                                      'price_prefix' => $price_prefix,
                                      'products_options_sort_order' => (int)$sort_order);

              if ( (isset($_POST['values_attributes_id'][$option_id][$value_id])) && (!empty($_POST['values_attributes_id'][$option_id][$value_id])) ){

                $attributes_id = (int)tep_db_prepare_input($_POST['values_attributes_id'][$option_id][$value_id]);

                if (tep_not_null($_POST['values_status'][$option_id][$value_id])) {
                  tep_db_perform(TABLE_PRODUCTS_ATTRIBUTES, $sql_data_array, 'update', "products_attributes_id = '" . (int)$attributes_id . "'");
                } else {
                  tep_db_query("delete from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_attributes_id = '" . $attributes_id . "'");
                }

              } else {
                if ( (isset($_POST['values_status'][$option_id][$value_id])) && (tep_not_null($_POST['values_status'][$option_id][$value_id])) ){
                  $sql_data_array['products_id'] = (int)$products_id;
                  $sql_data_array['options_id'] = (int)$option_id;
                  $sql_data_array['options_values_id'] = (int)$value_id;

                  tep_db_perform(TABLE_PRODUCTS_ATTRIBUTES, $sql_data_array);
                }
              }
            }
          }
        }
      }

 switch ($redirect) {
      case 'save_finish':
      $_SESSION['sess_option'] = '';
       tep_redirect(tep_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID']));

      case 'save_edit_product':
        $_SESSION['sess_option'] = '';
        tep_redirect(tep_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID']) . '&action=new_product');
      }
      break;
  }

//if attributes are new use then, if not check to see what has already been assigned and make them enabled
      $_SESSION['sess_option'] = array();
      if (isset($option_toggle) && is_array($option_toggle)) {
        $_SESSION['sess_option'] = $option_toggle;
      } else {
    //  create toggle options from product_attibutes
       $attributes_start_query_raw = "select distinct options_id from " . TABLE_PRODUCTS_ATTRIBUTES . "
                                      where
                                      products_id = '" . $products_id . "' ";
           $attributes_start_query = tep_db_query($attributes_start_query_raw);
           $option_toggle_id = 0;
              while ($attributes_start = tep_db_fetch_array($attributes_start_query)) {
              $option_toggle[] = $attributes_start['options_id'];
               }
    $_SESSION['sess_option'] = $option_toggle;
      }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script type="text/javascript" src="includes/menu.js"></script>
</head>
<body bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
<!-- options and values//-->
      <tr>
        <td width="100%"><table width="100%" border="0" cellspacing="0" cellpadding="5">
          <tr><?php echo tep_draw_form('attribute_edit', FILENAME_ATTRIBUTES, 'cPath=' . $cPath . '&pID=' . $products_id . '&action=edit'); ?>
            <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="2">

<?php
$products_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$products_id . "' and language_id = '" . (int)$languages_id . "'");

if ($products = tep_db_fetch_array($products_query)) {
  $heading_title = HEADING_TITLE . $products['products_name'];
} else {
  $heading_title = '';
}
?>
              <tr>
                <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
                  <tr>
                    <td class="pageHeading"><?php echo $heading_title; ?></td>
                    <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td><table border="0" cellspacing="3" cellpadding="3" align="center">
<?php
// read all the options to build the display
$options_query = tep_db_query("select po.products_options_id, pot.products_options_name, po.options_type from " . TABLE_PRODUCTS_OPTIONS . " po, " . TABLE_PRODUCTS_OPTIONS_TEXT  . " pot where pot.products_options_text_id = po.products_options_id and pot.language_id = '" . $languages_id . "' order by po.products_options_sort_order, pot.products_options_name");
while ($options = tep_db_fetch_array($options_query)) {
  $option_id = $options['products_options_id'];
  // build the table header
?>
                  <tr valign="top">
                    <td><table width="100%" border="0" cellpadding="2" cellspacing="2" style="border:1px solid #808080;">

      <tr class="dataTableHeadingRow">
<?php
$options_status = false;
if (is_array($_SESSION['sess_option']) && in_array($option_id, $_SESSION['sess_option'])) {
  $options_status = true;
}
if ($options_status) {
?>
                    <td class="dataTableHeadingContent" width="300"><?php echo tep_draw_checkbox_field('option_toggle[]', $options['products_options_id'], true) . '&nbsp;' . htmlspecialchars($options['products_options_name']); ?></td>
                    <td class="dataTableHeadingContent" width="50" align="center"><?php echo TABLE_HEADING_PRICE_PREFIX; ?></td>
                    <td class="dataTableHeadingContent" width="70" align="center"><?php echo TABLE_HEADING_PRICE; ?></td>
                    <td class="dataTableHeadingContent" width="70" align="center"><?php echo TABLE_HEADING_SORT_ORDER; ?></td>
<?php
    } else {
?>
                    <td class="dataTableHeadingContent" colspan="4"><?php echo tep_draw_checkbox_field('option_toggle[]', $options['products_options_id'], false) . '&nbsp;' . htmlspecialchars($options['products_options_name']); ?></td>
<?php
}
?>
       </tr>
<?php
if ($options_status) {
  if ( $options['options_type'] != 1 && $options['options_type'] != 4 ) {
  // now read all the possible values that go this option
  $values_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p where pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" . $option_id . "' and pov.language_id = '" . $languages_id . "' order by pov.products_options_values_name");

  $values_row = 0;
  $row = 0;
  while ($values = tep_db_fetch_array($values_query)) {
    $row++;
    $value_id = $values['products_options_values_id'];

    $products_values_query = tep_db_query("select products_attributes_id, price_prefix, options_values_price, products_options_sort_order from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . $option_id . "' and options_values_id = '" . (int)$value_id . "'");

    echo tep_draw_hidden_field('values[' . $option_id . '][' . $values_row . ']', $value_id) . "\n";

    if ($products_values = tep_db_fetch_array($products_values_query)) {
      echo tep_draw_hidden_field('values_attributes_id[' . $option_id . '][' . $value_id . ']', $products_values['products_attributes_id']) . "\n";
?>
                      <tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
                        <td class="smallText"><?php echo tep_draw_checkbox_field('values_status[' . $option_id . '][' . $value_id .']', '', true) . '&nbsp;' . htmlspecialchars($values['products_options_values_name']); ?>&nbsp;</td>
                        <td class="smallText" width="50" align="center"><?php echo tep_draw_input_field('values_price_prefix[' . $option_id . '][' . $value_id .']', $products_values['price_prefix'], 'size="2"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_price[' . $option_id . '][' . $value_id .']', $products_values['options_values_price'], 'size="7"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_sort_order[' . $option_id . '][' . $value_id .']', $products_values['products_options_sort_order'], 'size="7"'); ?></td>
                      </tr>
<?php
  } else {
?>
                      <tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
                        <td class="smallText"><?php echo tep_draw_checkbox_field('values_status[' . $option_id . '][' . $value_id .']', '', false) . '&nbsp;' . htmlspecialchars($values['products_options_values_name']); ?>&nbsp;</td>
                        <td class="smallText" width="50" align="center"><?php echo tep_draw_input_field('values_price_prefix[' . $option_id . '][' . $value_id .']', '', 'size="2"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_price[' . $option_id . '][' . $value_id .']', '', 'size="7"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_sort_order[' . $option_id . '][' . $value_id .']', '', 'size="7"'); ?></td>
                      </tr>

<?php
  }
  $values_row++;
  }
  } else {
    // for type 1 and 4, there is no options value, so we must dummy something up
    $values_row = 0;
    $row = 1;
    $value_id = 0;
    $values['products_options_values_name'] = TEXT_OPTION_TYPE_TEXTUAL;

    $products_values_query = tep_db_query("select products_attributes_id, price_prefix, options_values_price, products_options_sort_order from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . $option_id . "' and options_values_id = '" . (int)$value_id . "'");

    echo '<input type="hidden" name="values[' . $option_id . '][' . $values_row . ']" value="' . $value_id . '">' . "\n";

    if ($products_values = tep_db_fetch_array($products_values_query)) {
      echo tep_draw_hidden_field('values_attributes_id[' . $option_id . '][' . $value_id . ']', $products_values['products_attributes_id']) . "\n";
?>
                      <tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
                        <td class="smallText"><?php echo tep_draw_checkbox_field('values_status[' . $option_id . '][' . $value_id .']', '', true) . '&nbsp;' . htmlspecialchars($values['products_options_values_name']); ?>&nbsp;</td>
                        <td class="smallText" width="50" align="center"><?php echo tep_draw_input_field('values_price_prefix[' . $option_id . '][' . $value_id .']', $products_values['price_prefix'], 'size="2"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_price[' . $option_id . '][' . $value_id .']', $products_values['options_values_price'], 'size="7"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_sort_order[' . $option_id . '][' . $value_id .']', $products_values['products_options_sort_order'], 'size="7"'); ?></td>
                      </tr>
<?php
    } else {
?>
                      <tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
                        <td class="smallText"><?php echo tep_draw_checkbox_field('values_status[' . $option_id . '][' . $value_id .']', '', false) . '&nbsp;' . htmlspecialchars($values['products_options_values_name']); ?>&nbsp;</td>
                        <td class="smallText" width="50" align="center"><?php echo tep_draw_input_field('values_price_prefix[' . $option_id . '][' . $value_id .']', '', 'size="2"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_price[' . $option_id . '][' . $value_id .']', '', 'size="7"'); ?></td>
                        <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('values_sort_order[' . $option_id . '][' . $value_id .']', '', 'size="7"'); ?></td>
                      </tr>
<?php
    }
  }
}
?>
                    </table></td>
<?php
}
?>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main" align="center">
                <?php
       //when you view source in a browser name and value apear twice, but it is only way to get this to work
                echo tep_image_submit('button_save_finish.gif', IMAGE_SAVE_FINISH, 'name="submit" value="save_finish"') . '&nbsp;&nbsp;' .
                tep_image_submit('button_save_reload.gif', IMAGE_SAVE_RELOAD, 'name="submit" value="save_reload"') . '&nbsp;&nbsp;' .
                tep_image_submit('button_save_edit_product.gif', IMAGE_SAVE_EDIT_PRODUCT, 'name="submit" value="save_edit_product"') . '&nbsp;&nbsp;' .
                tep_image_submit('button_cancel.gif', IMAGE_CANCEL, 'name="submit" value="cancel"') . '&nbsp;&nbsp;' .
                tep_image_submit('button_cancel_edit_product.gif', IMAGE_CANCEL_EDIT_PRODUCT, 'name="submit" value="cancel_edit_product"');
?></td>
              </tr>
            </table></td>
          </form></tr>
        </table></td>
      </tr>
    </table></td>
<!-- products_attributes_eof //-->
  </tr>
</table>
<!-- body_text_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
